﻿// $ PURE $ // class name and mean

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CodeTable
{
    partial class GridData
    {
        public class Block
        {
            private Point gridCoordinate; // TODO: make more proper incapsulation
            private string dataCache = string.Empty;
            ScintillaNET.MarginCollection margins;
            private Size viewSize; // for optimization - to don't create views for every cell, store size of cell in data

            public event EventHandler TextChanged;

            public Block(Point gridCoordinate, ScintillaNET.MarginCollection margins)
            {
                this.gridCoordinate = gridCoordinate;
                this.margins = margins;
            }

            public string Text
            {
                get
                {
                    return this.dataCache;
                }
                set
                {
                    if (this.dataCache == value)
                        return;
                    this.dataCache = value;
                    viewSize = BlockAutosize.GetControlClientSize(this.dataCache, margins);
                    this.OnTextChanged(EventArgs.Empty);
                }
            }

            protected virtual void OnTextChanged(EventArgs e)
            {
                EventHandler handler = this.TextChanged;
                if (handler != null)
                {
                    handler(this, e);
                }
            }

            public Size TextSize
            {
                get
                {
                    return viewSize;
                }
            }

            public Point GridCoordinate
            {
                get
                {
                    return gridCoordinate;
                }
            }
        }
    }
}
